package com.ddwl.schema.serialize;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.RoundingMode;


public class DecimalSerializer implements ObjectSerializer {


    public final static InstantSerializer instance = new InstantSerializer();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeString("0.00");
            return;
        } else {
            out.writeString(((BigDecimal) object).setScale(2, RoundingMode.HALF_UP).toString());
        }
    }
}